<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if( !function_exists('mkdir_recursive')){
	function mkdir_recursive($pathname, $mode){
	    is_dir(dirname($pathname)) || mkdir_recursive(dirname($pathname), $mode);
	    return is_dir($pathname) || mkdir($pathname, $mode);
	}	
}

if( !function_exists('create_path')){
	function create_path($path, $mode = 0777){
		if( !is_dir($path) ){
			$p = mkdir($path, $mode, true);			// try using native function with recursive flag set to true
			if( !$p || !is_dir($path)){
				$p = mkdir_recursive( $path, $mode);	// use the forced recursive function
				if( !$p || !is_dir($path)){
					return false;
				}
			}
		}
		return true;
	}
}

if( !function_exists('copy_directory') ){
	function copy_directory($source, $dest){
		if( is_dir($source) ){
			if( create_path($dest) ){
				$d = opendir($source);
				if( $d ){
					while( ($f = readdir($d)) !== false){
						if( $f != '.' && $f != '..'){
							$sp = $source . '/' . $f;
							$dp = $dest . '/' . $f;
							if( is_dir($sp) ){
								copy_directory( $sp, $dp);
							}else{
								copy( $sp, $dp);
							}
						}
					}
				}
				closedir($d);
			}
		}
	}
}
?>
